package com.benzhuo.widget.pullableview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by WuWangchun on 2016/7/1.
 */
public class OneHeadView extends RelativeLayout implements IHeadView{

    // 释放刷新的距离
    private float refreshDist = 200;
    private int state = STATE_INIT;
    // 下拉的箭头
    private View pullView;
    // 正在刷新的图标
    private View refreshingView;
    // 刷新结果图标
    private View refreshStateImageView;
    // 刷新结果：成功或失败
    private TextView refreshStateTextView;
    private RotateAnimation rotateAnimation;
    private Context mContext;
    // 均匀旋转动画
	private RotateAnimation refreshingAnimation;

    public OneHeadView(Context context) {
        super(context);
        mContext = context;
    }

    public OneHeadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public OneHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public OneHeadView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    private void initView()
    {
        pullView = findViewById(R.id.pull_icon);
        refreshStateTextView = (TextView)findViewById(R.id.state_tv);
        refreshingView = findViewById(R.id.loading_icon);
        refreshStateImageView = findViewById(R.id.state_iv);
        rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(
                mContext, R.anim.vpp_reverse_anim);
        refreshingAnimation = (RotateAnimation) AnimationUtils.loadAnimation(
                mContext, R.anim.vpp_rotating);
        // 添加匀速转动动画
        LinearInterpolator lir = new LinearInterpolator();
        rotateAnimation.setInterpolator(lir);
        refreshingAnimation.setInterpolator(lir);
    }


    @Override
    public View getView() {
        return this;
    }

    @Override
    public float getDist() {
        return refreshDist;
    }

    @Override
    public void onTouchMove(float y) {
        if (y <= refreshDist
                && (state == STATE_PREPARE_LOAD || state == STATE_DONE))
        {
            // 如果下拉距离没达到刷新的距离且当前状态是释放刷新，改变状态为下拉刷新
            changeState(STATE_INIT);
        }
        if (y >= refreshDist && state == STATE_INIT)
        {
            // 如果下拉距离达到刷新的距离且当前状态是初始状态刷新，改变状态为释放刷新
            changeState(STATE_PREPARE_LOAD);
        }
    }

    private void changeState(int to) {
        state = to;
        switch (state)
        {
            case STATE_INIT:
                // 下拉布局初始状态
                refreshStateImageView.setVisibility(View.GONE);
                refreshStateTextView.setText(R.string.vpp_pull_to_refresh);
                pullView.clearAnimation();
                pullView.setVisibility(View.VISIBLE);
                break;
            case STATE_PREPARE_LOAD:
                // 释放刷新状态
                refreshStateTextView.setText(R.string.vpp_release_to_refresh);
                pullView.startAnimation(rotateAnimation);
                break;
            case STATE_LOADING:
                // 正在刷新状态
                pullView.clearAnimation();
                refreshingView.setVisibility(View.VISIBLE);
                pullView.setVisibility(View.INVISIBLE);
                refreshingView.startAnimation(refreshingAnimation);
                refreshStateTextView.setText(R.string.vpp_refreshing);
                break;
            case STATE_DONE:
                // 刷新或加载完毕，啥都不做
                break;
        }
    }


    @Override
    public void onTouchUp(float y) {
        pullView.clearAnimation();
        // 隐藏下拉头时有可能还在刷新，只有当前状态不是正在刷新时才改变状态
        if (state == STATE_PREPARE_LOAD){
            changeState(STATE_LOADING);
        }else if (state != STATE_LOADING){
            changeState(STATE_INIT);
        }
    }

    @Override
    public int getState() {
        return state;
    }

    @Override
    public void setState(int state) {
        changeState(state);
    }

    @Override
    public void setLoadResult(boolean success) {
        refreshingView.clearAnimation();
        refreshingView.setVisibility(View.GONE);
        if (success){
            refreshStateImageView.setVisibility(View.VISIBLE);
            refreshStateTextView.setText(R.string.vpp_refresh_succeed);
            refreshStateImageView
                    .setBackgroundResource(R.drawable.vpp_refresh_succeed);
        }else {
            refreshStateImageView.setVisibility(View.VISIBLE);
            refreshStateTextView.setText(R.string.vpp_refresh_fail);
            refreshStateImageView
                    .setBackgroundResource(R.drawable.vpp_refresh_failed);
        }
    }


}
